package com.apexedu.portal.service.impl;


import com.alibaba.fastjson.JSON;
import com.apexedu.biz.entity.TServiceSatisfaction;
import com.apexedu.biz.entity.client.Evaluation;
import com.apexedu.framework.dao.BaseDao;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.identity.entity.TSysUser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 山东大学门户
 *
 * @datetime 2016.06.25
 */
@Service("mhShdService")
public class MhShdServiceImpl  extends MhBaseServiceImpl {

    private static final Log LOG = LogFactory.getLog(MhShdServiceImpl.class);
    @Autowired
    BaseDao baseDao;

    /**
     * 服务评价
     * @param request
     * @param user
     * @param serviceSatisfaction
     * @return
     */
    public String evaluation(HttpServletRequest request,TSysUser user,TServiceSatisfaction serviceSatisfaction) {
        String json = "[]";
        Evaluation evaluation=new Evaluation();
        //1、判断用户本周评价了几次；
        String sql="select count(*) from T_Service_Satisfaction where menuid=? and cjr='"+user.getUserloginid()+"' and to_char(cjrq,'yyyy-MM-dd')=to_char(sysdate,'yyyy-MM-dd') ";
        int num= DBUtil.count(sql,serviceSatisfaction.getMenuid());
        if (num>=3){
            evaluation.setMessage("该服务您已评价！");
        }else{
            //2、记录评价内容：
            serviceSatisfaction.setWid(SeqFactory.getNewSequenceAlone());
            serviceSatisfaction.setCjrq(new Date());
            baseDao.saveOrUpdate(serviceSatisfaction);
            evaluation.setMessage("谢谢您的评价！");
        }
        //3、查询该服务当前评分；
        sql="select nvl(avg(satisfaction),3) as satisfaction from T_Service_Satisfaction where menuid=? ";
        TServiceSatisfaction temp=DBUtil.queryBean(sql,TServiceSatisfaction.class,serviceSatisfaction.getMenuid());
        if (temp!=null){
            evaluation.setScore(temp.getSatisfaction().toString());
        }
        try {
            json = JSON.toJSONString(evaluation);
        } catch (Exception e) {
            LOG.error(e);
        }
        return json;
    }
}
